package com.dzh.test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 戴志豪
 * @date 2022/10/8 20:57
 */
public class ArrayListDemo {


    /**
     * 集合流式操作
     * @param list  入参
     */
    public static void distinct1(List<Integer> list){
        List<Integer> integers = list.stream().distinct().sorted(Comparator.reverseOrder()).filter(i->i!=0).collect(Collectors.toList());
        for (Integer integer : integers) {
            if (integer.equals(integers.get(integers.size() - 1))) {
                System.out.print(integer);
                break;
            }
            System.out.print(integer + " ");
        }

    }


    /**
     * 判断是否存在
     * @param list  入参
     */
    public static void distinct2(List<Integer> list){
        List<Integer> list1 = new ArrayList<>();
        for (Integer integer : list) {
            if (list1.contains(integer)) {
                continue;
            }
            list1.add(integer);
        }
        for (Integer integer : list1) {

            if (integer.equals(list1.get(list1.size() - 2))) {
                System.out.print(integer);
                break;
            }
            System.out.print(integer + " ");
        }

    }

    /**
     * 利用set的去重特性
     * @param list  入参
     */
    public static void distinct3(List<Integer> list){
        Set<Integer> set = new HashSet<>(list);
        List<Integer> list1 = new ArrayList<>(set);

        for (Integer integer : list1) {

            if (integer.equals(list1.get(list1.size() - 2))) {
                System.out.print(integer);
                break;
            }
            System.out.print(integer + " ");
        }
    }



    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Integer> nums = new ArrayList<>();
        int n=1;
        while (n != 0){
            System.out.print("请输入n个数 （0 停止）：");
            n = scanner.nextInt();
            nums.add(n);
        }

//        distinct1(nums);
//        distinct2(nums);
          distinct3(nums);

    }




}
